package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.item.config.MqConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.PageDTO;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("hi")
    public String hi(){
        return "hi";
    }

    @GetMapping("/list")
    public PageDTO<Item> page(Integer page, Integer size){
        log.info("page==={}",page);
        log.info("size==={}",size);
        //Page<Item> page1 = new Page<>(page,size);
        Page<Item> page1 = new Page<>();
        //起始页
        page1.setCurrent(page);
        //查询条数
        page1.setSize(size);
        //执行分页
        Page<Item> itemPage = this.itemService.page(page1);
        //将获取的分页结果赋值到PageDTO中
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        //将分页数据赋值
        itemPageDTO.setList(itemPage.getRecords());
        //将分页数目赋值
        itemPageDTO.setTotal(itemPage.getTotal());
        return itemPageDTO;
    }
    @GetMapping("/{id}")
    public Item byItemId(@PathVariable("id") Long id){
        return this.itemService.getById(id);
    }

    /**
     * 添加
     * @param item
     */
    @PostMapping()
    public void save(@RequestBody Item item){
        this.itemService.save(item);
        //发送消息
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,item.getId());
        log.info("发送消息=={}",item.getId());
    }

    /**
     * 上下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void UpdateStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        log.info(id+":::::::"+status);
        LambdaUpdateWrapper<Item> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Item::getId,id);
        updateWrapper.set(Item::getStatus,status);
        this.itemService.update(updateWrapper);
        //发送消息
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,id);
    }

    /**
     * 修改数据
     * @param item
     */
    @PutMapping()
    public void update(@RequestBody Item item){
        this.itemService.updateById(item);
    }

    /**
     * 修改数据
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable("itemId") Long id,@PathVariable("num") Integer stock){
        //获取当前库存数量
        Item byId = this.itemService.getById(id);
        System.out.println("byId = " + byId);
        LambdaUpdateWrapper<Item> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Item::getId,id);
        updateWrapper.set(Item::getStock,byId.getStock()-stock);
        this.itemService.update(updateWrapper);
        //发送消息
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,id);
    }

    /**
     * 删除数据
     * @param id
     */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Long id){
        this.itemService.removeById(id);
        //发送消息
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_DELETE_KEY,id);
    }


}
